// pages/my/my.js
const app = getApp();   //获取小程序实例
import { get, getLikeMenu } from "../../utils/db"
Page({
  data: {
    userInfo: {}, //用户信息
    tag: false, //是否登录。 false 未登录  true，已经登录
    tabList: [
      { id: '1', title: '菜单' },
      { id: '2', title: '分类' },
      { id: '3', title: "关注" }
    ],
    active: 1,
    types: [],
    lists: [],
  },

  _delStyle() {
    wx.showModal({
      title: "删除提示",
      content: "确定要删除么？",
    })
  },
  // ----------------------------------------------------------------------------------------
  // 跳详情页
  nav() {
    wx.navigateTo({
      url: '../pbrecipe/pbrecipe',
    })
  },
  // -------------------------------------------------------------------------------------
  async onShow() {
    //如果有用户信息，直接渲染
    if (app.globalData.userInfo != null) {
      this.setData({
        userInfo: app.globalData.userInfo,
        tag: true
      })
    } else {
      //如果没有用户信息，从app.js中再获取用户信息
      app.getUserInfo = (res) => {
        this.setData({
          userInfo: res.userInfo,
          tag: true
        })
      }
    }
    //获取我添加的的菜单信息
    var openid = wx.getStorageSync("openid")
    var result = await get('menu', { _openid: openid })

    //获取我关注的菜单
    var likes = await get("likes", { _openid: openid })
    var arr = likes.data.map(item => {
      return item.menuId
    })
    var res = await getLikeMenu(arr)
    var lists = res.data;		//关注菜谱列表
    lists.forEach(item => {
      if (item.views == 0) {
        item.star = 0;
      } else if (item.views > 0 && item.views <= 10) {
        item.star = 1;
      } else if (item.views > 10 && item.views <= 20) {
        item.star = 2;
      } else if (item.views > 20 && item.views <= 30) {
        item.star = 3;
      } else if (item.views > 30 && item.views <= 40) {
        item.star = 4;
      } else {
        item.star = 5
      }
    })
    this.setData({
      recipes: result.data,
      lists
    })
  },
  // 我的关注
  async onLoad() {
    var result = await get("class");   //读取数据信息
    this.setData({
      types: result.data
    })
  },
  getInfo(e) {
    if (e.detail.userInfo !== undefined) {
      //拒绝
      this.setData({
        userInfo: e.detail.userInfo,
        tag: true
      })
    }
  },
  // 导航栏
  myClick(e) {
    this.setData({
      active: e.target.id
    })
  },
  //详情页
  toDetail(e) {
    wx.navigateTo({
      url: '../detail/detail?id=' + e.currentTarget.id,
    })
  },
  //跳转菜谱分类页面
  toType() {
    wx.navigateTo({
      url: '/pages/category/category',
    })
  },
  // 分类详情
  toList(e) {
    var classid = e.currentTarget.id
    console.log(classid);
    
    wx.navigateTo({
      url: '../list/list?id='+classid,
    })
  }
})